import { getUrlQuery } from "@/utils/format";
import { orderWriteOff } from "@/service/api/sp-order";
import { ORDER_STATUS } from "@/pages/sp-order/const/order";
import { PAGE_TYPE } from "@/pages/scan/const";

export default {
  methods: {
    async handlePubUserWriteOff(record) {
      const url = "?" + record?.detail?.result;
      const query = getUrlQuery(url);
      console.log("核销扫一扫", query, this.options);
      if (!this.isNoJumpToPage && query?.takeNo !== this.options?.takeNo) {
        return uni.showToast({
          icon: "none",
          title: "核销失败，请检查订单核销码是否正确",
        });
      }
      await this.handleFinishOrder(query?.takeNo);
    },
    async handleFinishOrder(takeNo) {
      const res = await orderWriteOff({ orderNoOrCode: takeNo }).catch(
        (err) => {
          uni.showToast({
            title: err.msg,
            icon: "none",
          });
        }
      );
      if (res?.code === 0) {
        uni.showToast({
          title: "核销成功",
          icon: "success",
        });
        const eventChannel = this.getOpenerEventChannel();
        eventChannel?.emit("orderStatus", {
          data: this.eventChannelOptions.orderNo,
        });
        setTimeout(() => {
          uni.navigateBack();
        }, 1500);
      }
    },
  },
};
